package com.bocai.kelalacar.ui.car.set.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.ApplyWayBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.set.contract.WithdrawTypeContract;
import com.bocai.kelalacar.ui.car.set.module.WithdrawTypeModule;
import com.bocai.kelalacar.ui.car.set.presenter.WithdrawTypePresenter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yuanfei on 2017/6/22.
 */

public class AddUnionPayActivity extends BaseActivity implements WithdrawTypeContract.view{
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ed_name)
    EditText edName;
    @BindView(R.id.ed_pay)
    EditText edPay;
    @BindView(R.id.add_name)
    EditText addName;
    @BindView(R.id.btn_add_union)
    Button btnAddUnion;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @Inject
    WithdrawTypePresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.act_add_union_pay;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new WithdrawTypeModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("提现方式");
        initBack(imgBack,llBack);
        btnAddUnion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(edName.getText().toString())){
                    showErrorTip("请输入持卡人姓名");
                }else if (TextUtils.isEmpty(edPay.getText().toString())){
                    showErrorTip("请输入卡号");
                }else if (TextUtils.isEmpty(addName.getText().toString())){
                    showErrorTip("请输入开户行");
                }else{
                    presenter.addApplyWay(addName.getText().toString(),"0",edName.getText().toString(),edPay.getText().toString());
                }
            }
        });
        edName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(edName.getText().toString())&&!TextUtils.isEmpty(edPay.getText().toString())
                        &&!TextUtils.isEmpty(addName.getText().toString())&&edPay.getText().toString().length()>16){
                    btnAddUnion.setEnabled(true);
                    btnAddUnion.setTextColor(getResources().getColor(R.color.color_white));
                }else {
                    btnAddUnion.setEnabled(false);
                    btnAddUnion.setTextColor(getResources().getColor(R.color.text_color));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        edPay.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(edName.getText().toString())&&!TextUtils.isEmpty(edPay.getText().toString())
                        &&!TextUtils.isEmpty(addName.getText().toString())&&edPay.getText().toString().length()>16){
                    btnAddUnion.setEnabled(true);
                    btnAddUnion.setTextColor(getResources().getColor(R.color.color_white));
                }else {
                    btnAddUnion.setEnabled(false);
                    btnAddUnion.setTextColor(getResources().getColor(R.color.text_color));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        addName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(edName.getText().toString())&&!TextUtils.isEmpty(edPay.getText().toString())
                        &&!TextUtils.isEmpty(addName.getText().toString())&&edPay.getText().toString().length()>16){
                    btnAddUnion.setEnabled(true);
                    btnAddUnion.setTextColor(getResources().getColor(R.color.color_white));
                }else {
                    btnAddUnion.setEnabled(false);
                    btnAddUnion.setTextColor(getResources().getColor(R.color.text_color));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void applyWaySuccess(ApplyWayBean bean) {

    }

    @Override
    public void addApplyWaySuccess() {
        showErrorTip("添加成功");
        setResult(19);
        finish();
    }

    @Override
    public void deleteApplyWaySuccess(int index) {

    }

    @Override
    public void walletApplySuccess() {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }
}
